package pers.qianyu.month_202102.date_20210202;

import org.junit.*;

/**
 * 424. 替换后的最长重复字符
 * https://leetcode-cn.com/problems/longest-repeating-character-replacement/
 *
 * @author mizzle rain
 * @date 2021-02-02 10:48
 */
public class CharacterReplacement2 {
    // 常规双指针做法，比较好理解
    public int characterReplacement(String s, int k) {
        int len = s.length();
        int left = 0, right = 0;
        int[] window = new int[26];
        int res = 0;
        while (right < len) {
            char c1 = s.charAt(right++);
            window[c1 - 'A']++;
            int max = 0;
            for (int i : window) if (i > max) max = i;
            if (right - left - max > k) window[s.charAt(left++) - 'A']--;
            else res = Math.max(right - left, res);
        }
        return res;
    }

    @Test
    public void test1() {
        int result = new CharacterReplacement2()
                .characterReplacement("AABABBA", 1);
        System.out.println("result = " + result);
    }

    @Test
    public void test2() {
        int result = new CharacterReplacement2()
                .characterReplacement("ABAB", 2);
        System.out.println("result = " + result);
    }
}
